package org.mspring.mlog.service;

import java.util.Date;
import java.util.List;

import org.mspring.mlog.dao.AlbumDao;
import org.mspring.mlog.entity.Album;
import org.mspring.mlog.entity.Photo;
import org.mspring.platform.dao.Page;
import org.mspring.platform.dao.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Gao Youbo
 * @since 2014-01-11
 */
@Service
@Transactional
public class AlbumService {

    @Autowired
    private AlbumDao albumDao;

    public List<Album> listAll() {
        return albumDao.listAll();
    }

    public Album get(Long id) {
        return albumDao.get(id);
    }

    public Page<Album> listPage(QueryBuilder queryBuilder, Page<Album> albumPage) {
        return albumDao.listPage(queryBuilder, albumPage);
    }

    public int getAlbumPhotoCount(long albumId) {
        return albumDao.getAlbumPhotoCount(albumId);
    }

    public void insert(Album album) {
        album.setCreateTime(new Date());
        album.setPhotoCount(0);
        albumDao.insert(album);
    }

    public void setCover(long albumId, long photoId) {
        Album album = get(albumId);
        if (album != null) {
            album.setCover(new Photo(photoId));
            update(album);
        }
    }

    public void update(Album album) {
        albumDao.update(album);
    }

    public void delete(Long... id) {
        albumDao.delete(id);
    }

    public void delete(Album album) {
        albumDao.delete(album);
    }
}
